<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="pname">
        <el-input
          v-model="queryParams.pName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据日期" prop="createDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择数据日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:data:add']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:data:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:data:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:data:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:data:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="pname" />
      <el-table-column label="完成情况" align="center" prop="details" />
      <el-table-column label="日进料量/吨" align="center" prop="oneDayInto" />
      <el-table-column label="畜禽粪便/吨" align="center" prop="animalFeces" />
      <el-table-column label="农作物秸秆/吨" align="center" prop="strawWaste" />
      <el-table-column label="农村易腐垃圾、公厕户厕粪污/吨" align="center" prop="rotten" />
      <el-table-column label="日沼气量/立方米" align="center" prop="dayMethane" />
      <el-table-column label="日产沼渣/吨" align="center" prop="dayBiogasResidue" />
      <el-table-column label="日发电量/度" align="center" prop="electricity" />
      <el-table-column label="日产沼液" align="center" prop="dayBiogasSlurry" />
      <el-table-column label="储存池日储存原料/吨" align="center" prop="dayStorageMaterials" />
      <el-table-column label="进料池日储存原料/吨" align="center" prop="intoMaterials" />
      <el-table-column label="日用气量/立方米" align="center" prop="dayGas" />
      <el-table-column label="供气户数/户" align="center" prop="provideUser" />
      <el-table-column label="数据日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:data:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:data:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="pname">
          <el-input v-model="form.pname" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="完成情况" prop="details">
          <el-input v-model="form.details" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="日进料量/吨" prop="oneDayInto">
          <el-input v-model="form.oneDayInto" placeholder="请输入日进料量/吨" />
        </el-form-item>
        <el-form-item label="畜禽粪便/吨" prop="animalFeces">
          <el-input v-model="form.animalFeces" placeholder="请输入畜禽粪便/吨" />
        </el-form-item>
        <el-form-item label="农作物秸秆/吨" prop="strawWaste">
          <el-input v-model="form.strawWaste" placeholder="请输入农作物秸秆/吨" />
        </el-form-item>
        <el-form-item label="农村易腐垃圾、公厕户厕粪污/吨" prop="rotten">
          <el-input v-model="form.rotten" placeholder="请输入农村易腐垃圾、公厕户厕粪污/吨" />
        </el-form-item>
        <el-form-item label="日沼气量/立方米" prop="dayMethane">
          <el-input v-model="form.dayMethane" placeholder="请输入日沼气量/立方米" />
        </el-form-item>
        <el-form-item label="日产沼渣/吨" prop="dayBiogasResidue">
          <el-input v-model="form.dayBiogasResidue" placeholder="请输入日产沼渣/吨" />
        </el-form-item>
        <el-form-item label="日发电量/度" prop="electricity">
          <el-input v-model="form.electricity" placeholder="请输入日发电量/度" />
        </el-form-item>
        <el-form-item label="日产沼液" prop="dayBiogasSlurry">
          <el-input v-model="form.dayBiogasSlurry" placeholder="请输入日产沼液" />
        </el-form-item>
        <el-form-item label="储存池日储存原料/吨" prop="dayStorageMaterials">
          <el-input v-model="form.dayStorageMaterials" placeholder="请输入储存池日储存原料/吨" />
        </el-form-item>
        <el-form-item label="进料池日储存原料/吨" prop="intoMaterials">
          <el-input v-model="form.intoMaterials" placeholder="请输入进料池日储存原料/吨" />
        </el-form-item>
        <el-form-item label="日用气量/立方米" prop="dayGas">
          <el-input v-model="form.dayGas" placeholder="请输入日用气量/立方米" />
        </el-form-item>
        <el-form-item label="供气户数/户" prop="provideUser">
          <el-input v-model="form.provideUser" placeholder="请输入供气户数/户" />
        </el-form-item>
        <el-form-item label="数据日期" prop="createDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择数据日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="importDialogVisible"
      title="导入用户卡"
      width="35%"
    >
      <el-dialog
        :visible.sync="innerImportDialogVisible"
        width="30%"
        title="系统提示"
        append-to-body>
        <el-row style="margin-bottom: 20px;">
          <el-col :span="4">总计:{{ allResult }}</el-col>
          <el-col :span="4">成功:{{ successResult }}</el-col>
          <el-col :span="4">失败:{{ errResult }}</el-col>
        </el-row>
        <el-row v-if="errResult && errResult > 0">
          <el-col :span="4"><el-button type="primary" @click="handleErrResult">导出错误信息</el-button></el-col>
        </el-row>
      </el-dialog>
      <div style="border:none;">
        <template>
          <span>{{ fileName }}</span>
          <el-upload
            class="upload-demo"
            ref="upload"
            action="/"
            :on-change="checkFileSure"
            :auto-upload="false"
            :show-file-list="false">
            <el-button slot="trigger" size="small" type="primary">上传execl</el-button>
            <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
          </el-upload>
        </template>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button :disabled="(!fileName)" :loading="importOptLoading" type="primary" @click="importExcel">导 入</el-button>
        <el-input v-model="currentCardId" type="hidden" />
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listData, getData, delData, addData, updateData, exportData, importExcel, importTemplate } from "@/api/system/biogasdata";

export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pName: null,
        details: null,
        oneDayInto: null,
        animalFeces: null,
        strawWaste: null,
        rotten: null,
        dayMethane: null,
        dayBiogasResidue: null,
        electricity: null,
        dayBiogasSlurry: null,
        dayStorageMaterials: null,
        intoMaterials: null,
        dayGas: null,
        provideUser: null,
        createDate: null
      },
      downloadLoading: false,
      importDialogVisible: false,
      innerImportDialogVisible: false,
      successResult: 0,
      errResult: 0,
      allResult: 0,
      importOptLoading: false,
      uploadForm: new FormData(),
      fileName: '',
      file: '',
      errList : [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oneDayInto: [
          { required: true, message: "日进料量/吨不能为空", trigger: "blur" }
        ],
        animalFeces: [
          { required: true, message: "畜禽粪便/吨不能为空", trigger: "blur" }
        ],
        strawWaste: [
          { required: true, message: "农作物秸秆/吨不能为空", trigger: "blur" }
        ],
        rotten: [
          { required: true, message: "农村易腐垃圾、公厕户厕粪污/吨不能为空", trigger: "blur" }
        ],
        dayMethane: [
          { required: true, message: "日沼气量/立方米不能为空", trigger: "blur" }
        ],
        dayBiogasResidue: [
          { required: true, message: "日产沼渣/吨不能为空", trigger: "blur" }
        ],
        electricity: [
          { required: true, message: "日发电量/度不能为空", trigger: "blur" }
        ],
        dayBiogasSlurry: [
          { required: true, message: "日产沼液不能为空", trigger: "blur" }
        ],
        dayStorageMaterials: [
          { required: true, message: "储存池日储存原料/吨不能为空", trigger: "blur" }
        ],
        intoMaterials: [
          { required: true, message: "进料池日储存原料/吨不能为空", trigger: "blur" }
        ],
        dayGas: [
          { required: true, message: "日用气量/立方米不能为空", trigger: "blur" }
        ],
        provideUser: [
          { required: true, message: "供气户数/户不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then(response => {
        debugger
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pname: null,
        details: null,
        oneDayInto: null,
        animalFeces: null,
        strawWaste: null,
        rotten: null,
        dayMethane: null,
        dayBiogasResidue: null,
        electricity: null,
        dayBiogasSlurry: null,
        dayStorageMaterials: null,
        intoMaterials: null,
        dayGas: null,
        provideUser: null,
        createDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateData(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addData(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delData(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有【请填写功能名称】数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportData(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleImport() {
      this.importDialogVisible = true
      this.importOptLoading = false
      this.$set(this, 'fileName', '')
      this.errList = []
    },
    checkFileSure(file) {
      this.file = file
      this.fileName = file.name
    },
    importExcel() {
      this.errList = []
      this.importOptLoading = true
      debugger
      importExcel(this.file.raw)
        .then(response => {
          const result = response.data
          this.errList = response.data.failResult
          var type = result.errno === 200 ? 'success' : 'error'
          if (result && result.data && result.data !== '') {
            this.successResult = result.data.success
            this.errResult = result.data.fail
            this.allResult = result.data.total
            this.errList = (result.data.fail === 0 ? [] : result.data.failResult)
            this.innerImportDialogVisible = true
          } else {
            this.$notify({
              title: '成功',
              type: type,
              message: response.data.errmsg
            })
          }
          this.importOptLoading = false
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
          this.importOptLoading = false
          this.errList = []
        })
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then((response) => {
        this.download(response.msg);
      });
    }
  }
};
</script>
